
(function (win) {
    if (win.require) {
        // 定义 require.js 执行加载时 模块名指向文件路径
        win.require.config({
            paths: {
              "ICalculator": "/Scripts/tools/ICalculator",
              "IIconSVG": "/Scripts/tools/IIconSVG",
            },
            waitSeconds: 15,
        });
    }
})(window as any);

/**
 * 将指定的资源文件路径转为对应的完整URL
 * @param resource_id 资源ID对象
 * @param need_merge_file_address 需要转化的文件路径
 * @returns 完整带域名的URL地址
 */
function ToResourceURL(resource_id: EResourceID, need_merge_file_address: string) {
    // console.log("window.location: ", window.location);
    let domain_name: string;
    let local_hostname_arr: string[] = ["localhost", "127.0.0.1"];
    let is_local: boolean = !window.location.host
        || local_hostname_arr.indexOf(window.location.hostname) > -1;
    if (is_local) {
        switch (resource_id) {
            case EResourceID.LibraryOne: domain_name = `localhost:17001`; break;
            default: throw new Error(`无法判断的资源点: ${resource_id}`);

        }
    } else if (window.location.host === "101.37.33.74:7007") {
        switch (resource_id) {
            case EResourceID.LibraryOne: domain_name = `101.37.33.74:7008`; break;
            default: throw new Error(`无法判断的资源点: ${resource_id}`);
        }
    }

    let protocol: string = window.location.protocol;
    if (protocol == "file:") {
        protocol = "http:";
    }
    let url_address = `${protocol}//${domain_name}${need_merge_file_address}`;
    let url = new URL(url_address);
    let v = generateRandomString(6);
    url.searchParams.append('v', v);
    return url.toString();
}

function generateRandomString(length: number) {
    var result = "";
    var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

/**
 * 检查并加载CSS文件（基础版）
 * @param {string[]} url_list 要加载的CSS文件路径队列
 */
function loadFile_CSS_Link(url_list: string[]) {
    // 获取所有link标签
    let links: HTMLCollectionOf<HTMLLinkElement> = document.getElementsByTagName("link");

    let link_hrefs: string[] = [];
    for (let i = 0; i < links.length; i++) {
        const link = links[i];
        if (link.rel === "stylesheet" && link.href) {
            link_hrefs.push(link.href);
        }
    }

    // 逐个排查目标列表
    for (let y = 0; y < url_list.length; y++) {
        const url: string = url_list[y] || "";

        let is_need_load: boolean = true;
        for (let i = 0; i < link_hrefs.length; i++) {
            const link_href: string = link_hrefs[i] || "";
            // 遍历检查是否已加载
            if (link_href === url || link_href.indexOf(url) > -1) {
                console.log(`CSS已存在: ${url}`);
                is_need_load = false;
                break;
            }
        }

        if (is_need_load) {
            // 如果未加载，则创建新的link标签
            var link: HTMLLinkElement = document.createElement("link");
            link.rel = "stylesheet";
            link.type = "text/css"
            link.href = url;
            document.getElementsByTagName("head")[0].appendChild(link);
            console.log(`CSS加载完成: ${url}`);
        }
    }
}
